<?php

namespace app\admin\controller;

use app\common\model\problem\BabycarProblem as BabycarProblemModel;

/**
 * TP Babycar 问题管理
 */
class BabycarProblem extends Controller
{
    /**
     * 获取问题类型列表
     * @return \think\response\Json
     */
    public function types()
    {
        return json([
            'code' => 0,
            'msg' => 'success',
            'data' => BabycarProblemModel::getProblemTypes()
        ]);
    }

    /**
     * 问题列表
     * @return \think\response\Json
     */
    public function list()
    {
        $params = $this->request->param();
        
        // 验证分页参数
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $page = max(1, $page);
        
        $model = new BabycarProblemModel();
        $list = $model->getList($params);
        
        return json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }

    /**
     * 问题详情
     * @param int $problem_id 问题ID
     * @return \think\response\Json
     */
    public function detail($problem_id)
    {
        $model = new BabycarProblemModel();
        $detail = $model->getDetail($problem_id);
        if (!$detail) {
            return json(['code' => 1, 'msg' => '问题不存在']);
        }
        return json(['code' => 0, 'msg' => 'success', 'data' => $detail]);
    }

    /**
     * 创建问题
     * @return \think\response\Json
     */
    public function create()
    {
        $data = $this->request->post();
        
        // 验证必填字段
        if (empty($data['type']) || empty($data['title']) || empty($data['description'])) {
            return json(['code' => 1, 'msg' => '请填写完整问题信息']);
        }

        // 验证问题类型
        if (!in_array($data['type'], BabycarProblemModel::getProblemTypes())) {
            return json(['code' => 1, 'msg' => '问题类型不正确']);
        }

        $model = new BabycarProblemModel();
        $result = $model->createProblem($data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '创建失败']);
        }
        return json(['code' => 0, 'msg' => '创建成功']);
    }

    /**
     * 更新问题
     * @param int $problem_id 问题ID
     * @return \think\response\Json
     */
    public function update($problem_id)
    {
        $data = $this->request->post();
        
        // 验证问题类型
        if (isset($data['type']) && !in_array($data['type'], BabycarProblemModel::getProblemTypes())) {
            return json(['code' => 1, 'msg' => '问题类型不正确']);
        }

        $model = new BabycarProblemModel();
        $result = $model->updateProblem($problem_id, $data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '更新失败']);
        }
        return json(['code' => 0, 'msg' => '更新成功']);
    }

    /**
     * 删除问题
     * @param int $problem_id 问题ID
     * @return \think\response\Json
     */
    public function delete($problem_id)
    {
        $model = new BabycarProblemModel();
        $result = $model->deleteProblem($problem_id);
        if (!$result) {
            return json(['code' => 1, 'msg' => '删除失败，问题可能不存在或已被删除']);
        }
        return json(['code' => 0, 'msg' => '删除成功']);
    }
} 